#define _CRT_SECURE_NO_WARNINGS	
#define _CRT_NONSTDC_NO_WARNINGS
#include <conio.h>
#include <windows.h>
#include<stdio.h>
#define Up 0
#define Dn 1
#define Lt 2
#define Rt 3

void hideCursor()
{
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO cursorInfo;
    GetConsoleCursorInfo(hOut, &cursorInfo);
    cursorInfo.bVisible = FALSE; // 隐藏光标
    SetConsoleCursorInfo(hOut, &cursorInfo);
}

void SpaceFrame( void )
{
	for( int i = 0; i < 30; i ++ )
	{
		for( int j = 0; j < 80; j ++ )
		{
			printf( " " );
		}
		printf( "\u2588\u2588\n" );
	}
	for( int i = 0; i < 82; i ++ )
		printf( "\u2588" );
	printf( "\n" );
}

void setCursorPos(int x, int y) 
{
	COORD coord;
	coord.X = x;
	coord.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

int GameBegin( char snake[ ][2] )
{
	int move = Rt;	//记录头运动方向，初始向右
	int Length = 4;
	char NextPos[ 2 ], LastPos[ 2 ];
	char ch;

	while( 1 )
	{
		NextPos[ 0 ] = snake[ 0 ][ 0 ];
		NextPos[ 1 ] = snake[ 0 ][ 1 ];
		if( kbhit() )
		{
			ch = getch();
			switch( ch )
			{
				case 'w':
				{
					if( move == Lt || move == Rt )
						move = Up;
					break;
				}
				case 's':
				{
					if( move == Lt || move == Rt )
						move = Dn;
					break;
				}
				case 'a':
				{
					if( move == Dn || move == Up )
						move = Lt;
					break;
				}
				case 'd':
				{
					if( move == Dn || move == Up )
						move = Rt;
					break;
				}
				case 27:
				{
					return 0;
				}
			}
		}
		
		switch( move )
		{
			case Rt:
			{
				snake[ 0 ][ 0 ] += 1;
				break;
			}
			case Lt:
			{
				snake[ 0 ][ 0 ] -= 1;
				break;
			}
			case Up:
			{
				snake[ 0 ][ 1 ] -= 1;
				break;
			}
			case Dn:
			{
				snake[ 0 ][ 1 ] += 1;
				break;
			}
		}
		
		if( snake[ 0 ][ 0 ] == -1 || snake[ 0 ][ 0 ] == 80 || snake[ 0 ][ 1 ] == 28 || snake[ 0 ][ 1 ] == -1 )
		{
			return 1;
		}
		for( int i = 1; i <= Length; i ++ )
		{
			if( snake[ 0 ][ 0 ] == snake[ i ][ 0 ] && snake[ 0 ][ 1 ] == snake[ i ][ 1 ] )
			{
				return 2;
			}
		}
		//这里理论上要先判断是否游戏结束，后面再写
		setCursorPos( NextPos[ 0 ], NextPos[ 1 ] );
		printf( " " );
		setCursorPos( snake[ 0 ][ 0 ], snake[ 0 ][ 1 ] );
		printf( "@" );
		for( int i = 1; i <= Length; i ++ )
		{
			setCursorPos( snake[ i ][ 0 ], snake[ i ][ 1 ] );
			printf( " " );
			LastPos[ 0 ] = snake[ i ][ 0 ];
			LastPos[ 1 ] = snake[ i ][ 1 ];
			snake[ i ][ 0 ] = NextPos[ 0 ];
			snake[ i ][ 1 ] = NextPos[ 1 ];
			NextPos[ 0 ] = LastPos[ 0 ];
			NextPos[ 1 ] = LastPos[ 1 ];
			setCursorPos( snake[ i ][ 0 ], snake[ i ][ 1 ] );
			printf( "*" );
		}
		Sleep( 1 );
		Sleep( 1 );
		Sleep( 1 );
		Sleep( 1 );
		
	}

	return 0;
}


int main()
{
	hideCursor();
	SetConsoleOutputCP( 65001 );
	char snake[ 900 ][ 2 ] = { -1 };
	snake[ 0 ][ 0 ] = 15;
	snake[ 0 ][ 1 ] = 15;
	snake[ 1 ][ 0 ] = 14;
	snake[ 1 ][ 1 ] = 15;
	snake[ 2 ][ 0 ] = 13;
	snake[ 2 ][ 1 ] = 15;
	snake[ 3 ][ 0 ] = 12;
	snake[ 3 ][ 1 ] = 15;
	snake[ 4 ][ 0 ] = 11;
	snake[ 4 ][ 1 ] = 15;
	
	int score = 0;	        //分数，吃到果子的个数，总是长度-1
	int over;
	

	SpaceFrame();
	over = GameBegin( snake );
	setCursorPos( 0, 0 );
	printf( "%d", over );

	return 0;
}